package com.cwt_taxi_xa;

import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.cwt_taxi_xa.constants.Constants;
import com.cwt_taxi_xa.model.AppInfo;
import com.cwt_taxi_xa.util.AppBiz;
import com.cwt_taxi_xa.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class SystemAppActivity extends CWTBaseActivity implements OnItemClickListener {
	private static final String TAG = "SystemAppActivity.";
	private GridView gridview = null;
	private List<AppInfo> mlistAppInfo = null;
	SystemAppAdapter systemeAppAdapter;
	PackageManager pm;
	private AppBiz appBiz;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.system_app);
		gridview = (GridView) findViewById(R.id.gridView);
		mlistAppInfo = new ArrayList<AppInfo>();
		pm = this.getPackageManager(); // 获得PackageManager对象
		queryAppInfo(); // 查询所有应用程序信息
		appBiz = new AppBiz(this);
		//gridview.setAdapter(systemeAppAdapter);
		gridview.setOnItemClickListener(this);

	}
	final Handler handler_refresh = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 101) {
				systemeAppAdapter = new SystemAppAdapter(SystemAppActivity.this, mlistAppInfo);
				gridview.setAdapter(systemeAppAdapter);
				//				systemeAppAdapter.notifyDataSetChanged();
			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	// 点击跳转至该应用程序
	public void onItemClick(AdapterView<?> arg0, View view, int position,
							long arg3) {
		// TODO Auto-generated method stub
		//		Intent intent = mlistAppInfo.get(position).getIntent();
		//		startActivity(intent);
		String pkgName = mlistAppInfo.get(position).getPkgName();
		appBiz.launchApp(pkgName);
	}
	// 获得所有启动Activity的信息，类似于Launch界面
	public void queryAppInfo() {
		ToastUtils.getInstance().popProgress(SystemAppActivity.this, "正在加载");
		new AsyncTask<String, Integer, Long>() {
			@Override
			protected Long doInBackground(String... params) {
				try {
					//					mlistAppInfo = appBiz.queryAppInfo();
					List<AppInfo> thiredApp = appBiz.queryFilterAppInfo(2);
					// 过滤
					if(thiredApp != null && thiredApp.size()>0){
						for(AppInfo app : thiredApp){
							if(appBiz.checkInstall(app.getPkgName())){
								int count = 0;
								String[] filterPkgNames = Constants.filterPkgNamesForRelease;
								for(String filter:filterPkgNames){
									if(app.getPkgName().equals(filter)){
										count ++;
									}
								}
								if(count == 0){
									mlistAppInfo.add(app);
								}
							}
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return 1L;
			}
			@Override
			protected void onPostExecute(Long result) {
				if(mlistAppInfo!=null&&mlistAppInfo.size()>0){
					Message message = new Message();
					message.what = 101;
					handler_refresh.sendMessage(message);
				}
				ToastUtils.getInstance().dismissCurrentDlg();
			}}.execute();

	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}
}
